import {
    getArtistList,
    getHotRecommend,
    getNewAlbum,
    getRecommendRanking,
    getTopBannners,
    getTopListPopular
} from "@/services"
import {
    createSlice,
    createAsyncThunk
} from "@reduxjs/toolkit"

export const fetchRecommendAction = createAsyncThunk("fetchrecommend", (playload, {
    dispatch
}) => {
    // 首页轮播图
    getTopBannners().then(res => {
        dispatch(changTopBanners(res.banners))
    })
    // 热歌推荐
    getHotRecommend(8).then(res => {
        dispatch(changeHotRecommends(res.result))
    })
    // 新碟上架
    getNewAlbum(10).then(res => {
        dispatch(changeNewAlbums(res.weekData))
    })
    // 榜单--飙升榜
    getRecommendRanking(19723756).then(res => {
        dispatch(changUpRankings(res.playlist))
    })
    // 榜单--新歌榜
    getRecommendRanking(3779629).then(res => {
        dispatch(changNewRanking(res.playlist))
    })
    // 榜单--原创榜
    getRecommendRanking(2884035).then(res => {
        dispatch(changOriginRanking(res.playlist))
    })
    // 入驻歌手
    getArtistList().then(res => {
        dispatch(changeArtistLists(res.artists))
    })
    // 热门主播
    getTopListPopular().then(res => {
        dispatch(changeTopListPopulars(res.data.list))
    })

})

const recommendSlice = createSlice({
    name: "recommend",
    initialState: {
        // 首页轮播图数据
        topBanners: [],
        // 热门推荐数据
        hotRecommends: [],
        // 新碟上架数据
        newAlbums: [],
        // 榜单数据
        rankings: {
            upRanking: {},
            newRanking: {},
            originRanking: {},
        },
        // 入驻歌手
        artistLists: [],
        // 热门主播
        topListPopular: []
    },
    reducers: {
        // 首页轮播图
        changTopBanners(state, { payload }) {
            state.topBanners = payload
        },
        // 热歌推荐
        changeHotRecommends(state, { payload }) {
            state.hotRecommends = payload
        },
        // 新碟上架
        changeNewAlbums(state, { payload }) {
            state.newAlbums = payload
        },
        // 榜单--飙升榜
        changUpRankings(state, { payload }) {
            state.rankings.upRanking = payload
        },
        // 榜单--新歌榜
        changNewRanking(state, { payload }) {
            state.rankings.newRanking = payload
        },
        // 榜单--新歌榜
        changOriginRanking(state, { payload }) {
            state.rankings.originRanking = payload
        },
        // 入驻歌手
        changeArtistLists(state, { payload }) {
            state.artistLists = payload
        },
        // 热门主播
        changeTopListPopulars(state, { payload }) {
            state.topListPopular = payload
        }
    }
})

export const {
    changTopBanners,
    changeHotRecommends,
    changeNewAlbums,
    changUpRankings,
    changNewRanking,
    changOriginRanking,
    changeArtistLists,
    changeTopListPopulars
} = recommendSlice.actions

export default recommendSlice.reducer